<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/25
 * Time: 13:36
 */

namespace app\adminapiv3\service;


class HostManager extends Base
{
    /**
     * @name 判断是否存在
     * @param $condition
     * @return bool
     * @author wx
     * @date 2019/10/25 13:54
     */
    function checkExist($condition)
    {
        $server = new \app\common\service\HostManager();
        $info = $server->findData($condition);

        if(!$info){
            return false;
        }

        return true;
    }

    /**
     * @name 列表
     * @param $params
     * @param int $limit
     * @return array
     * @author wx
     * @date 2019/10/25 14:08
     */
    function list($params, $limit = 10)
    {
        $server = new \app\common\service\HostManager();
        $list = $server->list($params, $limit);

        return $list;
    }

    /**
     * @name 添加
     * @param $addData
     * @return bool
     * @author wx
     * @date 2019/10/25 14:25
     */
    function add($addData)
    {
        //1. 检查部门是否存在
        $ministryServer = new HostMinistry();
        if (!$ministryServer->checkExist(['id' => $addData['ministry_id']])) {
            $this->setError($this->CODE_FAIL, '该部门不存在');
            return false;
        }

        //2. 检出重名
        if ($this->checkExist(['name' => $addData['name'], 'phone' => $addData['phone']])) {
            $this->setError($this->CODE_FAIL, '请勿重复添加责任人');
        }

        //3. 添加
        $addData['ctime'] = time();
        $server = new \app\common\service\HostManager();
        $server->add($addData);

        return true;
    }

    /**
     * @name 修改
     * @param $id
     * @param $updateData
     * @return bool
     * @author wx
     * @date 2019/10/25 14:25
     */
    function modify($id, $updateData)
    {
        //1. 检查部门是否存在
        $ministryServer = new HostMinistry();
        if (!$ministryServer->checkExist(['id' => $updateData['ministry_id']])) {
            $this->setError($this->CODE_FAIL, '该部门不存在');
            return false;
        }

        //2. 修改
        $server = new \app\common\service\HostManager();
        $server->modify(['id' => $id], $updateData);

        return true;
    }

    /**
     * @name 删除
     * @param $id
     * @return bool
     * @author wx
     * @date 2019/10/25 14:24
     */
    function del($id)
    {
        $server = new \app\common\service\HostManager();

        $condition[] = array('id', 'in', $id);
        $server->del($condition);

        return true;
    }

}